package com.hanqian.kepler.core.service.core.impl;

import cn.hutool.core.util.StrUtil;
import com.hanqian.kepler.common.bean.NameValueVo;
import com.hanqian.kepler.core.dao.primary.core.TargetDescDao;
import com.hanqian.kepler.core.entity.primary.core.TargetDesc;
import com.hanqian.kepler.core.service.core.TargetDescService;
import com.hanqian.kepler.core.service.flow.impl.BaseFlowServiceImpl;
import com.hanqian.kepler.flow.base.dao.BaseFlowDao;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;

/**
 * newFile
 * ============================================================================
 * author : dzw
 * createDate:  2020/12/24 。
 * ============================================================================
 */
@Service
public class TargetDescServiceImpl extends BaseFlowServiceImpl<TargetDesc> implements TargetDescService {

	@Autowired
	private TargetDescDao targetDescDao;
	@PersistenceContext
	private EntityManager em;

	@Override
	public BaseFlowDao<TargetDesc> getBaseFlowDao() {
		return targetDescDao;
	}

	@Override
	public List<NameValueVo> findTargetDescDictList(String targetId) {
		if(StrUtil.isBlank(targetId)) return new ArrayList<>();

		//String sql = "SELECT DISTINCT targetDescDict_id as \"value\",d.name as \"name\" FROM kepler_target_desc td,sys_dict d where td.targetDescDict_id=d.id and td.state='Enable' and td.target_id=\""+targetId+"\" order by d.sortNo asc";
		String sql = "SELECT DISTINCT targetDescDict_id as \"value\",d.name as \"name\" FROM kepler_target_desc td,sys_dict d where td.targetDescDict_id=d.id and td.state='Enable' and td.target_id=\""+targetId+"\"";
		Query query = em.createNativeQuery(sql);
		query.unwrap(SQLQuery.class).setResultTransformer(Transformers.aliasToBean(NameValueVo.class));
		return query.getResultList();
	}
}
